import { loginAPI } from "@/api/user"
import { setToken, getToken, removeToken } from "@/utils/auth"
export default {
    namespaced: true,
    state: {
        token: getToken() || ''
    },
    mutations: {
        //设置vuex中的toekn
        setToken(state, token) {
            // 设置vuex的token
            state.token = token
                // 把token写到浏览器的cookie中
            setToken(token)
        },
        removeToken(state) {
            state.token = ''
            removeToken() //清空cookie中的token
        }
    },
    actions: {
        async loginAction(context, data) {
            // async await:对内同步对外异步
            const res = await loginAPI(data)
                // console.log(res.data.token)
            context.commit('setToken', res.data.token)
            console.log(1)
        }
    }
}